package logic.common.team.processes;

import networking.messages.teamMessages.ProcessCreateMessage;
import fileHandling.language.Lang;
import gameStates.network.IDObject;
import logic.common.team.Team;
import main.listeners.ListenerContainer;
import main.listeners.ListenerKeeper;

public abstract class TeamProcess implements IDObject, ListenerKeeper {
	
	public enum ProcessType { Drone, Frigate, Carrier, MSWeapon, Upgrade }
	
	public static final float DRONE_TIME = 20f;
	public static final float FRIGATE_TIME = 70f;
	public static final float CARRIER_TIME = 50f;
	public static final float MS_WEAPON_TIME = 20f;
	public static final float UPGRADE_TIME = 70f;
	
	private int id;
	
	protected float processTime, currentTime;
	protected Team team;
	protected ListenerContainer listeners;
	protected int lastPercentage;
	protected boolean dummy;
	
	public TeamProcess(Team team, float processTime) {
		this.team = team;
		this.processTime = processTime;
		
		listeners = new ListenerContainer(ProcessListener.class);
	}
	
	public abstract void finish();
	
	public abstract Lang getDescription();
	
	public abstract ProcessCreateMessage getProcessCreateMessage();
	
	public void addProcessListener(ProcessListener l) { listeners.addListener(l); }
	
	public void removeProcessListener(ProcessListener l) { listeners.removeListener(l); }
	
	public void setDummy() { dummy = true; }
	
	public void update(float time) {
		currentTime += time;
		
		updateListeners();
		if(processTime <= currentTime) {
			if(!dummy) finish();
			team.teamProcessFinished(this);
		}
	}
	
	protected void updateListeners() {
		int percentage = (int)(currentTime / processTime * 100f);
		if(percentage != lastPercentage) {
			lastPercentage = percentage;
			ProcessUpdateEvent event = new ProcessUpdateEvent(this, percentage);
			listeners.callMethod(ProcessListener.PROCESSUPDATED, event);
		}
	}
	
	public void updateFromPercentage(int newPercentage) {
		currentTime = newPercentage * processTime / 100f;
		updateListeners();
	}
	
	public int getPercentage() { return lastPercentage; }
	
	public Team getTeam() { return team; }
	
	@Override
	public int getID() { return id; }
	
	@Override
	public void setID(int id) { this.id = id; }
	
	@Override
	public ListenerContainer getListenerContainer(Class<?> listenerClass) {
		if(listenerClass == ProcessListener.class) return listeners;
		return null;
	}
}